﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Process.BLL;
using Process.Entities;
using System.Text.RegularExpressions;
using KZ.Utilities;

namespace Provider
{
    public partial class LoginWebservice : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string type = Utility.StringParse(Request.QueryString["t"]).ToLower();
                switch (type)
                {
                    case "p":
                        dtgPartner.Visible = true;
                        dtgBroker.Visible = false;
                        LoadPartner();
                        break;
                    case "b":
                        dtgPartner.Visible = false;
                        dtgBroker.Visible = true;
                        LoadBroker();
                        break;
                }
            }
        }

        protected void btnLogin_Click(object sender, EventArgs e)
        {
            string type = Request.QueryString["t"].ToLower();
            string wsLink = String.Empty;
            string username = String.Empty;
            string password = String.Empty;
            int rowIndex = -1;

            CheckBox chk = new CheckBox();
            switch (type)
            {
                case "p":
                    foreach (GridViewRow row in dtgPartner.Rows)
                    {
                        chk = row.FindControl("chkSelect") as CheckBox;
                        if (chk != null && chk.Checked)
                        {
                            rowIndex = row.RowIndex; ;
                            string partnerID = Utility.StringParse(dtgPartner.DataKeys[row.RowIndex].Value);
                            PartnerENT partner = PartnerBLL.GetPartnerByID(partnerID);
                            wsLink = partner.WS1;
                            username = partner.BusinessUsername;
                            password = partner.BusinessPassword;
                            break;
                        }
                    }
                    break;
                case "b":
                    foreach (GridViewRow row in dtgBroker.Rows)
                    {
                        chk = row.FindControl("chkSelect") as CheckBox;
                        if (chk != null && chk.Checked)
                        {
                            rowIndex = row.RowIndex; ;
                            string brokerID = Utility.StringParse(dtgBroker.DataKeys[row.RowIndex].Value);
                            BrokerENT broker = BrokerBLL.GetBrokerByID(brokerID);
                            wsLink = broker.WS1;
                            username = broker.BusinessUsername;
                            password = broker.BusinessPassword;
                            break;
                        }
                    }
                    break;
            }

            if (rowIndex == -1)
            {
                if (type == "p")
                {
                    lblMessage.Text = "Bạn phải chọn nhà cung cấp để đăng nhập.";
                }
                else if (type == "b")
                {
                    lblMessage.Text = "Bạn phải chọn nhà môi giới để đăng nhập.";
                }
            }
            else
            {
                Login(type, wsLink, username, password);
            }
            message.Style["display"] = "block";
        }

        private void LoadPartner()
        {
            List<PartnerENT> partnerList = PartnerBLL.GetPartnerList();
            dtgPartner.DataSource = partnerList;
            dtgPartner.DataBind();

            btnLogin.Visible = partnerList.Count > 0;
        }

        private void LoadBroker()
        {
            List<BrokerENT> brokerList = BrokerBLL.GetBrokerList();
            dtgPartner.DataSource = brokerList;
            dtgBroker.DataBind();

            btnLogin.Visible = brokerList.Count > 0;
        }

        private void Login(string type, string wsLink, string username, string password)
        {
            Regex regex = new Regex(@"(.*)(\?op\=)(.*)");

            string ws = regex.Replace(wsLink, "$1");
            if (!ws.EndsWith("?wsdl")) { ws += "?wsdl"; }
            WebServiceInvoker invoker = new WebServiceInvoker(new Uri(ws));

            string method = regex.Replace(wsLink, "$3");

            try
            {
                object[] obj = { username, password };
                if (type == "p")
                {
                    Global.PartnerSID = invoker.InvokeMethod<string>(invoker.AvailableServices[0], method, obj);
                }
                else if (type == "b")
                {
                    Global.BrokerSID = invoker.InvokeMethod<string>(invoker.AvailableServices[0], method, obj);
                }
                lblMessage.Text = "Đăng nhập thành công.";
                message.Attributes.Add("class", "notification success png_bg message");
            }
            catch
            {
                lblMessage.Text = "Đăng nhập thất bại.";
                message.Style.Add("class", "error");
            }
        }
    }
}